package com.hjb.service.gateway.fuiou;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @author   liuze
 * @date 创建时间：2017年1月6日 下午2:39:31 
 * @version 1.0  
 */
public class FyPayUtils {

	
	/**
	 * 
	 * @author  liuze
	 * @date 2017年1月6日下午2:46:20
	 * @Description: 富友加签
	 * @param
	 */
	public  static  String   addSign(String signData,String pri_key){
		byte[] bytesKey;
		String RSA = null;
		try {
			bytesKey = (new BASE64Decoder()).decodeBuffer(pri_key.trim());
			PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(bytesKey);
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
			Signature signature = Signature.getInstance("MD5WithRSA");
			signature.initSign(priKey);
			signature.update(signData.getBytes("GBK"));
			RSA = (new BASE64Encoder()).encodeBuffer(signature.sign());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (SignatureException e) {
			e.printStackTrace();
		}
		
		
		return RSA;
	}
	
	public  static  boolean  checkSign(String signDataStr,String publicKey,String RSA){
		byte[] keyBytes;
		boolean ret =false;
		try {
			keyBytes = decryptBASE64(publicKey);
			// 构造X509EncodedKeySpec对象
			X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
			// KEY_ALGORITHM 指定的加密算法
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			// 取公钥匙对象
			PublicKey pubKey = keyFactory.generatePublic(keySpec);
			Signature signature = Signature.getInstance("MD5withRSA");
			signature.initVerify(pubKey);
			signature.update(signDataStr.getBytes("gbk"));
			// 验证签名是否正常
			ret = signature.verify(decryptBASE64(RSA));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public static byte[] decryptBASE64(String key) throws Exception {
		return (new BASE64Decoder()).decodeBuffer(key);
	}
	
	public static void main(String[] args) {
		String a ="pQwlbXpEww3uBzAVd1YZzyaimsw4%2FplVmyX5%2FU82x%2ByzV3LCHJiVHorTeGF0SNqiExAN9%2F1QsqXi%0D%0ANoKLRN%2BEhFw%2FumBu%2F81JoFWKFOHuqFo5AzQBFHN3Zaprbj8NSt2KbEhUgFitKT1CRdsGgML2qAlt%0D%0ARTphk1cppz0rbY4IW2g%3D%0D%0A";
		 try {
			byte[]  b  =  decryptBASE64(a);
			System.out.println(b.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static String nvl(String str){
		if(str==null){
			return "";
		}else{
			return str;
		}
	}
	
	
}
   